package com.example.NettyOnAndroidServer;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EchoServerHandler extends SimpleChannelInboundHandler<String> {

    private static ChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("SYSTEM CHANNEL ACTIVE: " + ctx.channel().remoteAddress());
        for (Channel channel : channels) {
            if (channel == incoming)
                incoming.writeAndFlush("[YOU] ACTIVE");
            else
                channel.writeAndFlush(incoming.remoteAddress() + " ACTIVE");
        }
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("SYSTEM CHANNEL RECEIVED: " + msg);
        for (Channel channel : channels) {
            if (channel == incoming) {
                System.out.println("[YOU]" + msg);
                incoming.writeAndFlush("[YOU]" + msg);
            } else {
                System.out.println("[" + incoming.remoteAddress() + "]" + msg);
                channel.writeAndFlush("[" + incoming.remoteAddress() + "]" + msg);
            }
        }
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("SYSTEM CHANNEL REMOVED: " + incoming.remoteAddress());
        for (Channel channel : channels) {
            if (channel == incoming)
                incoming.writeAndFlush("[YOU] OFFLINE");
            else
                channel.writeAndFlush("[" + incoming.remoteAddress() + "]" + " OFFLINE");
        }
        channels.remove(incoming);
        System.out.println("SYSTEM CHANNEL SIZE: " + channels.size());
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel incoming = ctx.channel();
        System.out.println("SYSTEM CHANNEL ADDED: " + incoming.remoteAddress());
        channels.add(incoming);
        System.out.println("SYSTEM CHANNEL SIZE: " + channels.size());

        for (Channel channel : channels) {
            if (channel == incoming)
                incoming.writeAndFlush("[YOU] ONLINE");
            else
                channel.writeAndFlush("[" + incoming.remoteAddress() + "]" + " ONLINE");
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}
